﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using Telerik.WebControls;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class SelectSales : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<SysRoleEntity> roles = new SystemBll().GetAll<SysRoleEntity>("");
                ddlRoles.DataSource = roles;
                ddlRoles.DataTextField = "RoleName";
                ddlRoles.DataValueField = "RoleID";
                ddlRoles.DataBind();

                ddlRoles.Items.Insert(0, new ListItem("全部", ""));
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string WhereClause = "";
            if (ddlRoles.SelectedValue != "")
            {
                WhereClause = " and UserID in (select UserID from Cms_Sys_UserRoleRelation where RoleID = '"+ddlRoles.SelectedValue+"')";
            }
            List<SysUserEntity> Users = new SystemBll().GetAll<SysUserEntity>(WhereClause," Order By username asc");
            gridUsers.DataSource = Users;
            gridUsers.DataBind();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            ContractBll bll = new ContractBll();
            foreach (GridDataItem item in gridUsers.Items)
            {
                CheckBox cbxSelect = item.FindControl("cbxSelect") as CheckBox; 
                if (cbxSelect != null)
                {
                    if (cbxSelect.Checked)
                    {
                        Guid UserID = new Guid(item.GetDataKeyValue("UserID").ToString());
                        CtrtCustomerUserEntity entity = bll.Get<CtrtCustomerUserEntity>(" and CustomerID = '" + Request.QueryString["CustomerID"] + "' and UserID = '" + UserID + "'");
                        if (entity == null)
                        {
                            entity = new CtrtCustomerUserEntity();
                            entity.CustomerID = new Guid(Request.QueryString["CustomerID"]);
                            entity.UserID = UserID;
                            entity.CreatedBy = base.CurrentUserID;
                            entity.CreatedAt = DateTime.Now;

                            bll.Insert(entity);
                        }
                    }
                }
            }

            ClientScript.RegisterStartupScript(this.GetType(), "success", "window.close();", true);

        }
    }
}
